package com.eascs.admin.o2o.creditarchive.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.admin.o2o.creditarchive.form.O2oCreditArchiveVo;
import com.eascs.admin.o2o.creditarchive.service.CreditArchiveService;
import com.eascs.admin.o2o.detail.service.LoanDetailService;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandService;
import com.eascs.admin.o2o.internal.util.BaseUtils;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanUserService;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.o2ozx.dao.O2oZxDao;
import com.eascs.admin.o2o.zx.entity.O2oLoanUserEntityZx;
import com.eascs.admin.o2o.zx.entity.O2oZxEntity;
import com.eascs.app.dao.PageResult;
import com.eascs.archive.entity.CreditArchiveRecordEntity;
import com.eascs.brand.service.O2oBrandService;
import com.eascs.brand.vo.BrandAndPartnerVo;
import com.eascs.common.util.StringUtil;
import com.eascs.file.FileTypeConstants;
import com.eascs.partner.entity.DealerPartnerEntity;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxConfirmButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;
import com.eascs.workflow.form.WorkFlowForm;

@RestController
@RequestMapping("/rcredit")
public class O2oCreditArchiveRestController extends BaseRestController {

	// private static final Logger logger =
	// LoggerFactory.getLogger(O2oCreditArchiveRestController.class);

	@Autowired
	private BaseUtils baseUtils;
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	@Autowired
	private O2oLoanUserService o2oLoanUserService;
	@Autowired
	private LoanapplyExpandService loanapplyExpandService;
	@Value("${o2o.admin.iframe.domain}")
	private String domain;
	@Autowired
	private LoanDetailService loanDetailService;
	@Autowired
	private O2oBrandService o2oBrandService;
	@Autowired
	private CreditArchiveService creditArchiveService;
	@Autowired
	private O2oZxDao o2oZxDao;
	@Autowired
	private O2oLoanapplyService o2oloanapplyService;

	@Value("${web.homeUrl}")
	String serviceHomeUrl;

	/**
	 * 征信归档详情页
	 * 
	 * @return
	 * @param
	 */
	@RequestMapping("/creditArchiveDetail")
	public BaseRestResult<String> creditArchiveDetail(O2oCreditArchiveVo entityVo) {	
		CreditArchiveRecordEntity creditArchiveRecordEntity = creditArchiveService.getCreditArchiveRecordById(entityVo.getReferId());
		if (creditArchiveRecordEntity != null) {
			ViewContext.put("vo", creditArchiveRecordEntity);
			O2oZxEntity zxEntity = o2oZxDao.getById(O2oZxEntity.class, creditArchiveRecordEntity.getLoanapplyNo());
			if(zxEntity!=null){
				ViewContext.put("zxEntity", zxEntity);
				O2oLoanUserEntityZx zxUserEntity = o2oZxDao.getLoanUserZx(zxEntity.getId());
				ViewContext.put("zxUserEntity", zxUserEntity);
			}
			//O2oLoanapplyEntity loanapply = o2oLoanapplyService.findContinueById(creditArchiveRecordEntity.getLoanapplyNo());
			String approvalUrl = baseUtils.getApprovalUrl(entityVo.getNodeAuditorId(), ProcessTemplate.SYSTEM_MODULE_O2O);// 审批代办列表详情URL
			approvalUrl += "&loadServiceUrl=" + serviceHomeUrl + "/work/jqUpload";
			String agreeUrl = baseUtils.getApprovalAgreeUrl(entityVo.getReferId(), entityVo.getNodeAuditorId(), ProcessTemplate.SYSTEM_MODULE_O2O);
//			BrandAndPartnerVo brand = o2oBrandService.queryCodeAndName(loanapply.getLoanapplyNo());
//			ViewContext.put("brand", brand);
//			String isNotContinue = null;
//			O2oLoanapplyEntity loanapplyEntity = loanDetailService.queryLoanapplyBillCode(loanapply.getLoanapplyNo(), loanapply.getLoanId(), loanapply.getBillCode(), loanapply.getCreateDate());
//			if (loanapplyEntity != null) {
//				loanapply.setContinueLoanParenNo(loanapplyEntity.getBillCode());
//				isNotContinue = "Y";
//			}
//			ViewContext.put("isNotContinue", isNotContinue);
//			ViewContext.put("loanapply", loanapply);
//			O2oLoanUserEntity materEntity = o2oLoanUserService.findSpouseId(loanapply.getLoanId(), loanapply.getLoanapplyNo());
//			O2oLoanUserEntity loanUser = o2oLoanUserService.findById(loanapply.getLoanId(), loanapply.getLoanapplyNo());
//			ViewContext.put("loanUser", loanUser);
//			ViewContext.put("materEntity", materEntity);
//			ViewContext.put("loanStatus", loanapplyExpandService.queryLoanapplyNo(creditArchiveRecordEntity.getLoanapplyNo()));
//			ViewContext.put("domain", domain);
			attachmentQuery(entityVo.getReferId());
			ViewContext.put("approvalStatus", entityVo.getApprovalStatus());// 流程经办状态是待办/已办/已发起，待办状态才显示审批栏
			if (("UNDO").equals(entityVo.getApprovalStatus())) {
				ViewContext.put("approvalUrl", approvalUrl);
				return HtmlRestResult.popHtmlFragment("approval/credit_archive_approval");
			} else {
				ViewContext.put("agreeUrl", agreeUrl);
				return HtmlRestResult.popHtmlFragment("approval/credit_archive_detail");
			}
		} else {
			return BaseRestResult.warn("征信归档信息不全，不能查看详情");
		}
	}
	
	/**
	 * @Description:
	 *               <p>
	 *               脱敏
	 *               </p>
	 * @author LMX
	 */
	@RequestMapping(value = "/credit_archive_detail_view")
	public HtmlRestResult creditArchiveDetailView(String referId) {
		CreditArchiveRecordEntity creditArchiveRecordEntity = creditArchiveService.getCreditArchiveRecordById(referId);
		if (creditArchiveRecordEntity != null) {
			O2oZxEntity zxEntity = o2oZxDao.getById(O2oZxEntity.class, creditArchiveRecordEntity.getLoanapplyNo());
			if(zxEntity!=null){
				O2oLoanUserEntityZx zxUserEntity = o2oZxDao.getLoanUserZx(zxEntity.getId());
				ViewContext.put("zxUserEntity", zxUserEntity);
			}
		}
		return HtmlRestResult.popHtmlFragment("/approval/credit_archive_detail_view");
	}
	
	public void attachmentQuery(String id) {
		PageResult<Object[]> pageResult = o2oloanapplyService.findO2oDataListEntityById(id);
		String titles[] = { "附件类型", "文件名", "提交人", "提交时间", "更新时间" };
		Button[] titleButtons = new Button[] { null,
				ButtonHelper.createOpenButton(null, "/file/download?datalistno=") };
		PaginationInfo datalistInfo = new PaginationInfo(pageResult, titles, "/loanUser/loanudpate");
		datalistInfo.setTitleButtons(titleButtons);
		datalistInfo.setBuildPagingNavigation(false);
		datalistInfo.setFirstRenderColumnIndex(1);
		datalistInfo.setBuildRowNumber(true);
		ViewContext.put("datalistInfo", datalistInfo);
	}

	/***
	 * 查询征信归档附件:
	 * 
	 * @param loanApplyNo
	 * @return
	 */
	@RequestMapping(value = "/datalist")
	public PaginationRestResult getOaBasicData(String loanApplyNo) {
		String titles[] = { "附件类型", "文件名", "提交人", "提交时间", "更新时间" };
		PageResult<Object[]> pageResult = o2oLoanapplyService.findO2oDataListEntityById(loanApplyNo, FileTypeConstants.DALIST_TYPE_CREDITFILING, FileTypeConstants.DALIST_TYPE_ZXFJ);
		Button[] titleButtons = new Button[] { null, ButtonHelper.createOpenButton(null, "/file/download?datalistno=") };
		PaginationInfo datalistInfo = new PaginationInfo(pageResult, titles, "");
		datalistInfo.setTitleButtons(titleButtons);
		datalistInfo.setBuildPagingNavigation(false);
		datalistInfo.setFirstRenderColumnIndex(1);
		datalistInfo.setBuildRowNumber(true);
		datalistInfo.setRendTo("#o2o_file");
		return PaginationRestResult.buildPaginationRestResult(datalistInfo);
	}

	/**
	 * 征信归档动态表单(平台节点)
	 * 
	 * @param workFlowForm
	 * @return
	 */
	@RequestMapping("/credit_archive_plat_form")
	public HtmlRestResult zxgd_plat(WorkFlowForm workFlowForm) {
		CreditArchiveRecordEntity creditArchiveRecordEntity = creditArchiveService.getCreditArchiveRecordById(workFlowForm.getReferId());
		ViewContext.put("creditArchiveRecordEntity", creditArchiveRecordEntity);
		return HtmlRestResult.html("workflowcheck/credit_archive_plat_form");
	}

	/**
	 * 征信归档动态表单(省区节点)
	 * 
	 * @param workFlowForm
	 * @return
	 */
	@RequestMapping("/credit_archive_province_form")
	public HtmlRestResult zxgd_province(WorkFlowForm workFlowForm) {
		CreditArchiveRecordEntity creditArchiveRecordEntity = creditArchiveService.getCreditArchiveRecordById(workFlowForm.getReferId());
		ViewContext.put("creditArchiveRecordEntity", creditArchiveRecordEntity);
		return HtmlRestResult.html("workflowcheck/credit_archive_province_form");
	}

	/**
	 * 征信归档动态表单(总部档案管理员)
	 *
	 * @param workFlowForm
	 * @return
	 */
	@RequestMapping("/credit_archive_admin_form")
	public HtmlRestResult zxgd_archiveAdmin(WorkFlowForm workFlowForm) {
		CreditArchiveRecordEntity creditArchiveRecordEntity = creditArchiveService.getCreditArchiveRecordById(workFlowForm.getReferId());
		ViewContext.put("creditArchiveRecordEntity", creditArchiveRecordEntity);
		return HtmlRestResult.html("workflowcheck/credit_archive_admin_form");
	}

	/**
	 * ajax 刷新征信归档凭证
	 * 
	 * @param referId loanapplyNo
	 * @param code
	 * @return
	 */
	@RequestMapping("/credit_archive_file")
	public HtmlRestResult creditFile(String referId, String code) {
		queryDataList(referId, FileTypeConstants.DALIST_TYPE_CREDITFILING, null);
		ViewContext.put("referId", referId);
		return HtmlRestResult.html("/workflowcheck/credit_archive_file");
	}

	public void queryDataList(String loanapplyNo, String code, String type) {
		PageResult<Object[]> pageResult = o2oLoanapplyService.findO2oDataListEntityById(loanapplyNo, "'" + code + "'");
		String titles[] = { "附件类型", "文件名", "提交人", "提交时间", "更新时间" };
		Button[] titleButtons = new Button[] { null, ButtonHelper.createOpenButton(null, "/file/download?datalistno=") };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles);
		AjaxConfirmButton[] dealButton = null;
		if (type == null) {
			dealButton = new AjaxConfirmButton[] { new AjaxConfirmButton("删除 ", "/loanrest/delete/") };
		} else if (type.equals("NS_XDJL")) {
			dealButton = new AjaxConfirmButton[] { new AjaxConfirmButton("删除 ", "/loanrest/delete/") };
		}
		paginationInfo.setMoreButtons(dealButton);
		paginationInfo.setTitleButtons(titleButtons);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setBuildRowNumber(true);
		ViewContext.put("pageResult", paginationInfo);// 个人附件
	}

	/**
	 * 征信归档凭证保存
	 * 
	 * @param o2oCreditArchiveVo
	 * @return
	 */
	@RequestMapping("/credit_archive_save")
	public BaseRestResult<String> creditSave(O2oCreditArchiveVo o2oCreditArchiveVo) {

		if (null != o2oCreditArchiveVo && ("plat").equals(o2oCreditArchiveVo.getType())) {
			if (o2oCreditArchiveVo.getAuthBookCount() == 0) {
				return BaseRestResult.warn("授权书份数不能为空");
			}
			if (o2oCreditArchiveVo.getIdCardCopies() == 0) {
				return BaseRestResult.warn("身份证复印件份数不能为空");
			}
			if (StringUtil.isEmpty(o2oCreditArchiveVo.getExpressCompanyPlatform())) {
				return BaseRestResult.warn("平台快递公司不能为空");
			}
			if (StringUtil.isEmpty(o2oCreditArchiveVo.getExpressNoPlatform())) {
				return BaseRestResult.warn("平台快递单号不能为空");
			}
			if (StringUtil.isEmpty(o2oCreditArchiveVo.getReceiverPlatform())) {
				return BaseRestResult.warn("平台收件人不能为空");
			}
		}
		if (null != o2oCreditArchiveVo && ("province").equals(o2oCreditArchiveVo.getType())) {
			if (StringUtil.isEmpty(o2oCreditArchiveVo.getExpressCompanyProvince())) {
				return BaseRestResult.warn("省区快递公司不能为空");
			}
			if (StringUtil.isEmpty(o2oCreditArchiveVo.getExpressNoProvince())) {
				return BaseRestResult.warn("省区快递单号不能为空");
			}
			if (StringUtil.isEmpty(o2oCreditArchiveVo.getReceiverProvince())) {
				return BaseRestResult.warn("省区收件人不能为空");
			}
		}
		creditArchiveService.updateCreditArchive(o2oCreditArchiveVo);
		return BaseRestResult.warn("保存成功");
	}

}
